from api import Detector
from PIL import Image
import numpy as np
import time
import cv2
import matplotlib.pyplot as plt

# Initialize detector
detector = Detector(model_name='rapid',
                    weights_path='./weights/pL1_MWHB1024_Mar11_4000.ckpt')
# 使用视频数据
#capture=cv2.VideoCapture("./video/MW-18Mar-19.avi")
#capture=cv2.VideoCapture("./video/MW-18Mar-18_320x240.3gp2")
capture1=cv2.VideoCapture("./video/MW-18Mar-18.avi")
capture2=cv2.VideoCapture("./video/MW-18Mar-18_320x240.3gp2")
# 设置内容
image_resize_scale = (1080,1080)
use_our_image = False     # 使用自己的视频时，需要将这个选项改成True
fps = 0.0

while(True):

    # 读取某一帧
    ref1,frame1=capture1.read()
    ref2,frame2=capture2.read()

    # 转变成Image
    frame1 = Image.fromarray(np.uint8(frame1))
    frame2 = Image.fromarray(np.uint8(frame2))
    # resize 图像
    frame1 = frame1.resize(image_resize_scale, Image.ANTIALIAS)
    frame2 = frame2.resize(image_resize_scale, Image.ANTIALIAS)

    frame1 = np.array(detector.detect_one(pil_img=frame1,
                        input_size=1024, conf_thres=0.3,
                        return_img=True))
    frame2 = np.array(detector.detect_one(pil_img=frame2,
                          input_size=1024, conf_thres=0.3,
                          return_img=True))

    out_win1 = "video_full_screen"
    cv2.namedWindow(out_win1, cv2.WINDOW_NORMAL)
    cv2.setWindowProperty(out_win1, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
    cv2.imshow(out_win1,frame1)

    out_win2 = "video_full_screen_change_resolution"
    cv2.namedWindow(out_win2, cv2.WINDOW_NORMAL)
    cv2.setWindowProperty(out_win2, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
    cv2.imshow(out_win2,frame2)

    c = cv2.waitKey(2) & 0xff
    if c == 27:
        #capture1.release()
        #capture2.release()
        break
